<?php

namespace App\Http\Controllers\Admin;

use App\Models\Attention;
use App\Models\AttentionRecords;
use App\Models\Lesson;
use App\Models\Classes;
use App\Models\Student;
use App\Models\Type;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ExcelExport;

/**
 * 用户分类管理
 * Class TypeController
 * @package App\Http\Controllers\Admin
 */
class AttentionController extends BaseController
{
    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $list = Attention::paginate(config('site.other.paginate'));

        return backendView('attention.index', compact('list'));
    }
    public function record(Request $request)
    {
        $record = new AttentionRecords();
        $query = $request->only('g_code', 'c_code', 'a_code','s_code');
        $classes = [];$students = [];
        $admin = $this->getAdmin();
        if(!empty($admin->tc_code)) {
            $class = Classes::where('tc_code', $admin->tc_code)->first();
            if (!empty($class)) {
                $record = $record->where('c_code', $class->c_code);
                $students = Student::where('c_code', $class->c_code)->get();
            } else {
                $record = $record->where('c_code', '0');
            }
        } else {
            $classes = Classes::all();
            $students = Student::all();
        }
        if(!empty($query['c_code'])) {
            $record = $record->where('c_code', $query['c_code']);
        }
        if(!empty($query['g_code'])) {
            $record = $record->where('g_code', $query['g_code']);
        }
        if(!empty($query['a_code'])) {
            $record = $record->where('a_code', $query['a_code']);
        }
        if(!empty($query['s_code'])) {
            $record = $record->where('s_code', $query['s_code']);
        }
        $list = $record->orderBy('id', 'desc')->paginate(config('site.other.paginate'));
        $list = $list->appends($request->input());

        $attentions = Attention::all();
        $grades = AttentionRecords::$grades;
        return backendView('attention.record', compact('list','query','attentions','classes','grades','students'));
    }
    public function export(Request $request)
    {
        $record = new AttentionRecords();
        $query = $request->only('g_code', 'c_code', 'a_code','s_code');
        $file_n = '';
        if(!empty($query['c_code'])) {
            $file_n .= '-';
            $record = $record->where('c_code', $query['c_code']);
        }
        if(!empty($query['g_code'])) {
            $file_n .= '-'.AttentionRecords::$grades[$query['g_code']];
            $record = $record->where('g_code', $query['g_code']);
        }
        if(!empty($query['a_code'])) {
            $file_n .= '-';
            $record = $record->where('a_code', $query['a_code']);
        }
        if(!empty($query['s_code'])) {
            $file_n .= '-';
            $record = $record->where('s_code', $query['s_code']);
        }
        $list = $record->orderBy('id', 'desc')->get();
        $out[] = ['类型','年级','班级','学生','指数','记录日期'];
        if (!empty($list)) {
            foreach($list as $item) {
                $out[] = [
                    $item->attention->title,
                    $item->grade,
                    $item->class->name,
                    $item->student->name,
                    $item->indicators,
                    $item->record_date,
                ];
            }
        }

        $export = new ExcelExport($out);
        $file = '专注度记录' . '.xlsx';
        return Excel::download($export, $file);
    }
    /**
     * 新增分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'l_code' => 'required|max:255|unique:lesson,l_code',
                'name' => 'required|max:255',
            ], [], [
                'l_code' => __("code"),
                'name' => __("Name"),
            ]);

            $data = $request->only(['name','l_code']);
            //$data['tl_code'] = rand(100000,999999);
            if (Lesson::create($data)) {
                return redirect()->route('admin.lesson.index')->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            return backendView('lesson.add');
        }
    }

    /**
     * 编辑分类
     * @param Request $request
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request, $id = null)
    {

        $lesson = Lesson::find($id);
        if (empty($lesson)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => ['required', 'max:255', Rule::unique('types')->ignore($lesson->id)],
                'l_code' => 'required|max:255',
            ], [], [
                'name' => '分类名称',
                'l_code' => __("code"),
            ]);

            $data = $request->only(['name','l_code']);

            if ($lesson->update($data)) {
                return redirect()->route('admin.lesson.index')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            return backendView('lesson.edit', compact('lesson'));
        }
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function view($id = null)
    {
        /**
         * @var $type Type
         */
        $type = Type::find($id);
        if (empty($type)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        return backendView('type.view', compact('type'));
    }

    /**
     * 删除分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        /**
         * @var $type Type
         */
        $lesson = Lesson::find($id);
        if (empty($lesson)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        try {
//            $users = $type->users()->get();
//            $agencies = $type->agencies()->get();
//
//            if (count($users) > 0 || count($agencies) > 0) {
//                return back()->with('error', '已使用的分类不能删除');
//            }

            if ($lesson->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }
}
